package com.ccp.dev.qbdms.service;

import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdFalProblemresetDao;
import com.ccp.dev.qbdms.model.vo.TopFiveProblemVo;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProcessingTimeUtil;
import com.ccp.dev.system.service.SysOrgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zzy on 2020/7/21.
 *
 * @Author zzy
 * @Date 2020/7/21
 * @describe
 */
@Service
public class TopFiveProblemService {
    @Resource
    public WQbdFalProblemresetDao wQbdFalProblemresetDao;
    @Resource
    public SysOrgService sysOrgService;
    @Resource
    public ScriptImpl scriptimpl;


    /**
     *  获取前五位数据
     * @param start 开始日期
     * @param end   结束日期
     * @param sysorgid  统计单位
     * @param productname   故障产品名称
     * @return
     */
    public List<Map<String, Object>> getData(Date start,Date end,String sysorgid,String productname) {
        Map<String, Object> querymap = ProcessingTimeUtil.converSimpleDateRange("", start, end);
        //选择要统计的单位
        querymap.put("orgcode", sysorgid);
        querymap.put("productname", productname);
        querymap.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
        ZhiLiangConst.addReportCodeConditions(querymap);

        List<Map<String, Object>> list = new ArrayList<>();
        if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.GROUPLEVEL) {
            list = wQbdFalProblemresetDao.getTop5ByFilldepthospitalid(querymap);
        }
        else {
            list = wQbdFalProblemresetDao.getTop5ByFilldeptiid(querymap);
        }
        return list;
    }

    /**
     *  将map转为bean
     * @param queryFilter
     * @return
     */
    public List<TopFiveProblemVo> exportExcel(Date start, Date end, String sysorgid, String productname){
        List<Map<String, Object>> odatalist = getData(start, end, sysorgid, productname);
        List<TopFiveProblemVo> datalist = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : odatalist) {
            TopFiveProblemVo t = new TopFiveProblemVo();
            t.setORGNAME(stringObjectMap.get("ORGNAME").toString());
            t.setCPMC(stringObjectMap.get("CPMC").toString());
            t.setNUMS(stringObjectMap.get("NUMS").toString());
            t.setZB(stringObjectMap.get("ZB").toString());
            datalist.add(t);
        }
        return datalist;
    }

}
